package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.*;
import com.ruoyi.service.IErpPurchaseOrderManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service

public class OrderDetailBomImpl extends ServiceImpl<OrderDetailBomMapper, OrderDetailBom> implements com.ruoyi.service.OrderDetailBomImpl {
    @Autowired
    OrderDetailBomMapper orderDetailBomMapper;
    @Autowired
    OrderDetailBomMapper2 orderDetailBomMapper2;
    @Autowired
    ErpPurchaseRequestManagementMapper pur;
    @Autowired
    ErpPurchaseOrderManagementMapper erpPurchaseOrderManagementMapper;

    @Override
    public boolean addmaterials(List<OrderDetailBom2> ids, int key) {
//        List<OrderDetailBom> orderDetailBoms = new ArrayList<>();
//// 创建Map用于快速查找已存在的物料项
//        Map<Integer, OrderDetailBom> materialMap = new HashMap<>();
//
//        for (OrderDetailBom sourceItem : ids) {
//            Integer materialId = sourceItem.getId();
//
//            if (materialMap.containsKey(materialId)) {
//                // 物料已存在，累加价格和数量
//                OrderDetailBom existingItem = materialMap.get(materialId);
//                existingItem.setPrice(existingItem.getPrice().add(sourceItem.getPrice()));
//                existingItem.setQuantity(existingItem.getQuantity().add(sourceItem.getQuantity()));
//            } else {
//                // 物料不存在，创建新条目
//                OrderDetailBom newItem = new OrderDetailBom();
//                newItem.setMaterialId(materialId);
//                newItem.setOrderId(key);
//                newItem.setPrice(sourceItem.getPrice());
//                newItem.setQuantity(sourceItem.getQuantity());
//
//                orderDetailBoms.add(newItem);
//                materialMap.put(materialId, newItem); // 登记到Map中
//            }
//
//
//        }
//        return true;
        System.err.println("选择物料     = " + ids);
        // 2. 查询order_item中所有已有的物料数据
        List<OrderDetailBom2> orderDetailBoms = orderDetailBomMapper2.selectList(null);
        //获取 所有 material_id
        Set<Integer> materialIdSet = orderDetailBoms.stream()
                .map(OrderDetailBom2::getMaterialId)
                .collect(Collectors.toSet());

        ids.forEach(bom -> {
            if (materialIdSet.contains(bom.getId())) {
                LambdaUpdateWrapper<OrderDetailBom2> queryWrapper = new LambdaUpdateWrapper<>();
                queryWrapper.eq(OrderDetailBom2::getMaterialId,bom.getId());
                int rows = orderDetailBomMapper2.delete(queryWrapper);
                OrderDetailBom2 newItem = new OrderDetailBom2();
                System.out.println("新增的物料id = " + bom.getId());
                newItem.setMaterialId(bom.getId());
                newItem.setOrderId(key);
                newItem.setPrice(bom.getPrice());
                newItem.setQuantity(bom.getQuantity());
                orderDetailBomMapper2.insert(newItem);

//                // 存在：获取价格和数量
//                System.out.println("获取物料 " + bom.getMaterialId() +
//                        " 的价格: " + bom.getPrice() +
//                        ", 数量: " + bom.getQuantity());
//                LambdaUpdateWrapper<OrderDetailBom2> wrapper = new LambdaUpdateWrapper<>();
//                LambdaQueryWrapper<OrderDetailBom2> lambdaQueryWrapper=new LambdaQueryWrapper();
//                lambdaQueryWrapper.eq(OrderDetailBom2::getMaterialId,bom.getMaterialId());
//                OrderDetailBom2 orderDetailBom = orderDetailBomMapper2.selectOne(lambdaQueryWrapper);
//                BigDecimal price = orderDetailBom.getPrice();
//                BigDecimal quantity = orderDetailBom.getQuantity();
////                需要更新的数目
//                BigDecimal totalQuantity = orderDetailBom.getQuantity().add(bom.getQuantity());
//                wrapper.eq(OrderDetailBom2::getMaterialId, bom.getMaterialId()).set(OrderDetailBom2::getQuantity,totalQuantity);
//                orderDetailBomMapper2.update(null,wrapper);
////                修改总价格
//                LambdaQueryWrapper<ErpPurchaseOrderManagement>zongjiage=new LambdaQueryWrapper<>(
//                );
//                zongjiage.eq(ErpPurchaseOrderManagement::getId,bom.getOrderId());
//
//                ErpPurchaseOrderManagement erpPurchaseOrderManagement = erpPurchaseOrderManagementMapper.selectOne(zongjiage);
//                int totalprice = erpPurchaseOrderManagement.getTotalprice();
//                BigDecimal totalAmount = price.multiply(quantity);
//                BigDecimal result = totalAmount.add(new BigDecimal(totalprice));
//                LambdaUpdateWrapper<ErpPurchaseOrderManagement> orderDetailBomLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
//                orderDetailBomLambdaUpdateWrapper.eq(ErpPurchaseOrderManagement::getId,bom.getOrderId()).set(ErpPurchaseOrderManagement::getTotalprice,result);
//            erpPurchaseOrderManagementMapper.update(null,orderDetailBomLambdaUpdateWrapper);


            } else {
                // 不存在：新增


                OrderDetailBom2 newItem = new OrderDetailBom2();
                System.out.println("新增的物料id = " + bom.getId());
                newItem.setMaterialId(bom.getId());
                newItem.setOrderId(key);
                newItem.setPrice(bom.getPrice());
                newItem.setQuantity(bom.getQuantity());

                orderDetailBomMapper2.insert(newItem);
                System.out.println("新增物料: " + bom.getItemId());

            }
        });
        return true;

    }












    @Override
    public List<OrderDetailBom> orderwuliaoId(int id) {
        List<OrderDetailBom> orderwuliaoId = orderDetailBomMapper.orderwuliaoId(id);
        System.out.println("采购单内容如下" + orderwuliaoId);

        if (orderwuliaoId != null) {
            System.out.println("查询成功");
            return orderwuliaoId;
        }
        return orderwuliaoId;
    }


}